package com.jhhe.homework.MyActor

import akka.actor.{Actor, ActorRef, ActorSystem, Props}

/**
 * @author: jhhe
 * @date: 2021-03-18
 *        作业要求：
 *        1、编写 2 个 Actor ，分别是 AActor 和 BActor
 *        2、AActor 和 BActor 之间可以相互发送消息
 */
class AActor extends Actor{
  override def receive: Receive = {
    case 0 => println("我打")
    case 10 => {
      context.stop(self)
      context.system.terminate()
    }
    case i: Int => println(s"AActor（黄飞鸿）：挺猛，看我佛山无影脚...第$i 脚")
  }
}

class BActor extends Actor{
  override def receive: Receive = {
    case 0 => println("start OK!")
    case 10 => context.stop(self)
    case i: Int =>  println(s"AActor（乔峰）：挺猛，看降龙十八掌...第$i 掌")
  }
}

object MyActor {
  private val myFactory = ActorSystem("myFactory")
  private val AActorRef: ActorRef = myFactory.actorOf(Props[AActor], "AActor")
  private val BActorRef: ActorRef = myFactory.actorOf(Props[BActor], "BActor")
  var flag = 0
  def main(args: Array[String]): Unit = {
    println("AActor 出招了...")
    while (flag<=10){
      BActorRef ! flag
      AActorRef ! flag
      flag += 1
    }
    Thread.sleep(100)
    println("比赛结束")
  }
}
